//
//  NSDate+YC.m
//  YCSchool
//
//  Created by gmall on 16/5/4.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//  日期 

#import "NSDate+YC.h"
#import "NSCalendar+YC.h"

@implementation YCDateInterval

@end

/**
 *  说明:HH:24小时  hh:12小时  MM:月份 mm:分钟
 */

@implementation NSDate (YC)

/********* 新的 begin *********/

/**
 *  判断是否为今年
 *
 *  @return 是与否
 */
- (BOOL)yc_isInThisYear
{
    NSCalendar *calendar = [NSCalendar yc_calendar];
    
    NSDateComponents *nowCmps = [calendar components:NSCalendarUnitYear fromDate:[NSDate date]];
    NSDateComponents *selfCmps = [calendar components:NSCalendarUnitYear fromDate:self];
    return [nowCmps isEqual:selfCmps];
}

/**
 *  判断是否为今天
 *
 *  @return 是与否
 */
- (BOOL)yc_isInToday
{
    NSCalendar *calendar = [NSCalendar yc_calendar];
    
    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
    NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]];
    NSDateComponents *selfCmps = [calendar components:unit fromDate:self];
    
    return [nowCmps isEqual:selfCmps];
}

/**
 *  判断是否为昨天
 *
 *  @return 真或假
 */
- (BOOL)yc_isInYesterday
{
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"yyyyMMdd";
    
    NSString *selfString = [fmt stringFromDate:self]; // 20150131
    NSString *nowString = [fmt stringFromDate:[NSDate date]]; // 20150201
    
    NSDate *selfDate = [fmt dateFromString:selfString];
    NSDate *nowDate = [fmt dateFromString:nowString];
    
    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
    NSDateComponents *cmps = [[NSCalendar yc_calendar] components:unit fromDate:selfDate toDate:nowDate options:0];
    
    return cmps.year == 0
    && cmps.month == 0
    && cmps.day == 1;
}
/**
 *  判断时否为明天
 *
 *  @return 返回真与假
 */
- (BOOL)yc_isInTomorrow
{    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"yyyyMMdd";
    
    NSString *selfString = [fmt stringFromDate:self];
    NSString *nowString = [fmt stringFromDate:[NSDate date]];
    
    NSDate *selfDate = [fmt dateFromString:selfString];
    NSDate *nowDate = [fmt dateFromString:nowString];
    
    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
    NSDateComponents *cmps = [[NSCalendar yc_calendar] components:unit fromDate:selfDate toDate:nowDate options:0];
    
    return cmps.year == 0
    && cmps.month == 0
    && cmps.day == -1;
}
//返回当前分
+ (NSInteger)yc_getMinuteIntegerFromDateString:(NSString *)dateString formatter:(NSString *)formetter
{
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = formetter;
    fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh"];
    fmt.timeZone = [NSTimeZone localTimeZone];
    
    NSDate *date = [fmt dateFromString:dateString];
    NSCalendarUnit unit = NSCalendarUnitMinute;
    NSDateComponents *cmps = [[NSCalendar yc_calendar] components:unit fromDate:date];
    return cmps.minute;
}
//返回当前时
+ (NSInteger)yc_getHourIntegerFromDateString:(NSString *)dateString formatter:(NSString *)formetter
{
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = formetter;
    fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh"];
    fmt.timeZone = [NSTimeZone localTimeZone];
    
    NSDate *date = [fmt dateFromString:dateString];
    NSCalendarUnit unit = NSCalendarUnitHour;
    NSDateComponents *cmps = [[NSCalendar yc_calendar] components:unit fromDate:date];
    return cmps.hour;
}

//返回当前日
+ (NSInteger)yc_getDayIntegerFromDateString:(NSString *)dateString formatter:(NSString *)formetter
{
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = formetter;
    fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh"];
    fmt.timeZone = [NSTimeZone localTimeZone];
    
    NSDate *date = [fmt dateFromString:dateString];
    NSCalendarUnit unit = NSCalendarUnitDay;
    NSDateComponents *cmps = [[NSCalendar yc_calendar] components:unit fromDate:date];
    return cmps.day;
}

//返回当前月
+ (NSInteger)yc_getMonthIntegerFromDateString:(NSString *)dateString formatter:(NSString *)formetter
{
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = formetter;
    fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh"];
    fmt.timeZone = [NSTimeZone localTimeZone];
    
    NSDate *date = [fmt dateFromString:dateString];
    NSCalendarUnit unit = NSCalendarUnitMonth;
    NSDateComponents *cmps = [[NSCalendar yc_calendar] components:unit fromDate:date];
    return cmps.month;
}

//返回当前年
+ (NSInteger)yc_getYearIntegerFromDateString:(NSString *)dateString formatter:(NSString *)formetter
{
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = formetter;
    fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh"];
    fmt.timeZone = [NSTimeZone localTimeZone];
    
    NSDate *date = [fmt dateFromString:dateString];
    NSCalendarUnit unit = NSCalendarUnitYear;
    NSDateComponents *cmps = [[NSCalendar yc_calendar] components:unit fromDate:date];
    return cmps.year;
}


/**
 *  求与另一个时间的时间差
 *
 *  @param anotherDate 本身与另一个时间的时间差
 *
 *  @return 时间差模型
 */
- (YCDateInterval *)yc_intervalSinceDate:(NSDate *)anotherDate
{
    // 时间间隔(单位:s)
    NSInteger interval = [self timeIntervalSinceDate:anotherDate];
    
    NSInteger secsPerMinute = 60;
    NSInteger secsPerHour = 60 * secsPerMinute;
    NSInteger secsPerDay = 24 * secsPerHour;
    
    YCDateInterval *dateInterval = [[YCDateInterval alloc] init];
    dateInterval.day = interval / secsPerDay;
    dateInterval.hour = (interval % secsPerDay) / secsPerHour;
    dateInterval.minute = ((interval % secsPerDay) % secsPerHour) / secsPerMinute;
    dateInterval.second = interval % secsPerMinute;
    return dateInterval;
}
/**
 *  求与另一个时间的时间差(指针传值)
 *
 *  @param anotherDate 另一个时间对象
 *  @param dayP        天(地址)
 *  @param hourP       时(地址)
 *  @param minuteP     分(地址)
 *  @param secondP     秒(地址)
 */
- (void)yc_intervalSinceDate:(NSDate *)anotherDate day:(NSInteger *)dayP hour:(NSInteger *)hourP minute:(NSInteger *)minuteP second:(NSInteger *)secondP
{
    // 时间间隔(单位:s)
    NSInteger interval = [self timeIntervalSinceDate:anotherDate];
    
    NSInteger secsPerMinute = 60;
    NSInteger secsPerHour = 60 * secsPerMinute;
    NSInteger secsPerDay = 24 * secsPerHour;
    
    *dayP = interval / secsPerDay;
    *hourP = (interval % secsPerDay) / secsPerHour;
    *minuteP = ((interval % secsPerDay) % secsPerHour) / secsPerMinute;
    *secondP = interval % secsPerMinute;
}


/**
 *  日期处理,根据时间字符串,返回昨天今天几小时前等字符串
 */
+ (NSString *)yc_dateStringWithAddTimeString:(NSString *)addtime_str formatter:(NSString *)formatter
{
    NSString *newString;
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = formatter;
    NSDate *createdAtDate = [fmt dateFromString:addtime_str];
    if (createdAtDate.yc_isInThisYear) { // 今年
        if (createdAtDate.yc_isInYesterday) { // 昨天
            fmt.dateFormat = @"昨天 HH:mm:ss";
            newString = [fmt stringFromDate:createdAtDate];
        } else if (createdAtDate.yc_isInToday) { // 今天
            NSDateComponents *cmps = [[NSCalendar yc_calendar] components:NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:createdAtDate toDate:[NSDate date] options:0];
            if (cmps.hour >= 1) { // 时间间隔 >= 1小时
                newString = [NSString stringWithFormat:@"%zd小时前", cmps.hour];
            } else if (cmps.minute >= 1) { // 1小时 > 时间间隔 >= 1分钟
                newString = [NSString stringWithFormat:@"%zd分钟前", cmps.minute];
            } else { // 1分钟 > 时间间隔
                newString = @"刚刚";
            }
        } else { // 今年的其他日子
            fmt.dateFormat = @"MM-dd HH:mm:ss";
            newString = [fmt stringFromDate:createdAtDate];
        }
    } else { // 非今年
        newString = addtime_str;
    }
    
    return newString;
}

/********* 新的 end *********/

- (NSDate *)yc_dateWithYMD {
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"yyyy-MM-dd";
    NSString *selfStr = [fmt stringFromDate:self];
    return [fmt dateFromString:selfStr];
}


- (NSDateComponents *)yc_deltaWithNow {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    int unit = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    return [calendar components:unit fromDate:self toDate:[NSDate date] options:0];
}

+ (NSString *)yc_timestampStrWithDate:(NSDate *)date
{
    NSDate *newDate = nil;
    if (date == nil) {
        newDate = [NSDate date];
    } else {
        newDate = date;
    }
    return [NSString stringWithFormat:@"%zd", (NSInteger)[newDate timeIntervalSince1970]];
}

+ (NSString *)yc_createDate:(NSDate *)createdDate {
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"YYYY-MM-dd HH:mm:ss"; //日期格式化
    if (createdDate.yc_isInToday) {
        fmt.dateFormat = @"HH:mm";
        return [fmt stringFromDate:createdDate];
    } else if (createdDate.yc_isInYesterday) { // 昨天
        fmt.dateFormat = @"MM-dd HH:mm";
        return [fmt stringFromDate:createdDate];
    } else if (createdDate.yc_isInThisYear) { // 今年(至少是前天)
        fmt.dateFormat = @"MM-dd HH:mm";
        return [fmt stringFromDate:createdDate];
    } else { // 非今年
        fmt.dateFormat = @"yy-MM-dd HH:mm";
        return [fmt stringFromDate:createdDate];
    }
}

+ (NSString *)yc_timestampStrWithDateStr:(NSString *)dateStr format:(NSString *)format
{
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = format;
    NSDate *date = [fmt dateFromString:dateStr];
    NSString *timeSp = [NSString stringWithFormat:@"%ld",(long)[date timeIntervalSince1970]];
    return timeSp;
}

+ (NSString *)yc_createDateWithYYYYMMDD:(NSDate *)createdDate
{
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"YYYY-MM-dd HH:mm:ss"; //日期格式化
    if (createdDate.yc_isInToday) {
        fmt.dateFormat = @"yyyy年MM月dd日";
        return [fmt stringFromDate:createdDate];
        
    } else if (createdDate.yc_isInYesterday) { // 昨天
        fmt.dateFormat = @"yyyy年MM月dd日";
        return [fmt stringFromDate:createdDate];
    } else if (createdDate.yc_isInThisYear) { // 今年(至少是前天)
        fmt.dateFormat = @"yyyy年MM月dd日";
        return [fmt stringFromDate:createdDate];
    } else { // 非今年
        fmt.dateFormat = @"yyyy年MM月dd日";
        return [fmt stringFromDate:createdDate];
    }
    
}

/**
 *  时间戳转字符串时间
 *
 *  @param time 时间戳
 *
 *  @return 字符串日期,YYYY年MM月DD日
 */
+ (NSString *)yc_transTimeWithTimeString:(NSString *)time
{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[time integerValue]];
    return [NSDate yc_createDate:date];
}


- (NSString *)yc_dateStringWithFormatString:(NSString *)formatString
{
    NSString *result;
    if(formatString)
    {
        NSDateFormatter *formatter = [NSDateFormatter new];
        formatter.dateFormat = formatString;
        result = [formatter stringFromDate:self];
    }
    return result;
}

@end
